<template>
  <div class="box">
    <div class="login">
      <div class="left">
        <img src="../../../src/assets/images/login/login.png" alt="">
      </div>
      <div class="right">
        <el-form label-position="top" label-width="100px" :model="formLabelAlign" style="max-width: 460px">
          <h3>欢迎登录创思设备管理系统</h3>
          <el-form-item label="邮箱">
            <el-input v-model.trim="formLabelAlign.email" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model.trim="formLabelAlign.password" type="password" show-password />
          </el-form-item>
          <div class="password">
            <el-checkbox v-model="flag">记住我</el-checkbox>
            <el-link @click="onReset" :underline="false">忘记密码</el-link>
          </div>
          <el-button @click="login" type="info">登录</el-button>
          <span>你还没有账号？<el-link @click="onRegister" type="primary" :underline="false">立即注册</el-link></span>
        </el-form>
      </div>
    </div>
  </div>

</template>

<script>
import { ElMessage } from 'element-plus'
import { reactive, ref, getCurrentInstance, onMounted } from 'vue'
import { useRouter } from 'vue-router'
// import axios from 'axios'
import md5 from 'md5'
export default {
  setup() {
    const { proxy } = getCurrentInstance()
    const router = useRouter()
    // 表单的双向绑定数据
    const formLabelAlign = reactive({
      email: '',
      password: ''
    })
    // checkbox状态
    const flag = ref(false)
    const login = async () => {
      // console.log(md5(formLabelAlign.password));
      await proxy.$http.post('user/login', {
        email: formLabelAlign.email,
        password: md5(formLabelAlign.password)
      }).then(({ data: res }) => {
        // console.log(res);
        if (res.code !== 200) return ElMessage({
          message: res.msg,
          type: 'error'
        })
        // 判断当前"记住我"的选中状态
        if (flag.value) {
          localStorage.setItem('lastToken', formLabelAlign.email)
        }
        localStorage.setItem('token', formLabelAlign.email)
        ElMessage({
          message: '登录成功',
          type: 'success'
        })
        router.push({
          name: 'home'
        })
      })
    }
    // 跳转到注册页面
    const onRegister = () => {
      router.push({
        name: 'register'
      })
    }
    // 跳转到重置密码页面
    const onReset = () => {
      router.push({
        name: 'reset'
      })
    }
    // 判断用户上次登录是否点击了记住密码
    const lastEmail = () => {
      const email = localStorage.getItem('lastToken')
      if (!email) return
      formLabelAlign.email = email
    }
    onMounted(() => {
      lastEmail()
    })
    return {
      flag,
      formLabelAlign,
      login,
      onRegister,
      onReset,
      lastEmail
    }
  }
}
</script>

<style lang="less" scoped>
.box {
  position: absolute;
  top: 25%;
  left: 28%;
  width: 45%;
  height: 60%;
  min-width: 400px;
  min-height: 400px;
  max-width: 720px;
  max-height: 512px;

  .login {
    display: flex;
    width: 100%;
    height: 100%;

    .left {
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: #9A9AEB;
      width: 50%;
      height: 100%;

      img {
        width: 80%;
      }
    }

    .right {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 50%;
      height: 100%;
      background-color: #fff;

      .password {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }
  }
}


.el-button {
  width: 100%;
  margin-top: 5%;
  margin-bottom: 5%;
}

.el-form {
  margin: 20% 10%;

  h3 {
    text-align: center;
  }

  span {
    // text-align: center;
    margin-left: 60px;
    font-size: 14px;

    .el-link__inner {
      font-size: 14px;
    }
  }
}
</style>